﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InSafetyAirClient.GUI.Plane
{
    public partial class UpdateP : UserControl
    {

        List<InSafetyAirClient.InSafetyAirService.Plane> planes = null;
        List<InSafetyAirClient.InSafetyAirService.PlaneModel> models = null;
        AutoCompleteStringCollection autoComplete = new AutoCompleteStringCollection();

        public UpdateP()
        {
            InitializeComponent();
        }

        private void UpdateP_Load(object sender, EventArgs e)
        {
            if(!this.DesignMode)
            {
                populateList();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ServiceSingle servS = ServiceSingle.Instance;
            foreach (InSafetyAirService.Plane plane in planes)
            {
                if (plane.registrationNo.Equals(comboBox1.Text) && textBox1.Text != "")
                {
                    plane.registrationNo = textBox1.Text;
                    foreach (InSafetyAirService.PlaneModel planeM in models)
                    {
                        if (planeM.planeModelName.Equals(comboBox2.Text) && textBox1.Text != "")
                        {
                            plane.planeModelId = planeM.planeModelId;
                            servS.getService().UpdateToDatabase(plane);
                            populateList();
                        }
                        else
                            Console.Beep();
                    }
                }
                else
                    Console.Beep();
            }
        }

        private void populateList()
        {
            ServiceSingle servS = ServiceSingle.Instance;
            //
            //Search reg
            //
            planes = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.Plane).Name).OfType<InSafetyAirClient.InSafetyAirService.Plane>().ToList();
            comboBox1.Items.Clear();
            comboBox2.Items.Clear();
            foreach (InSafetyAirService.Plane plane in planes)
            {
                comboBox1.Items.Add(plane.registrationNo);
                autoComplete.Add(plane.registrationNo);
            }
            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox1.AutoCompleteCustomSource = autoComplete;
            //
            //new model
            //
            models = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.PlaneModel).Name).OfType<InSafetyAirClient.InSafetyAirService.PlaneModel>().ToList();
            foreach (InSafetyAirService.PlaneModel planeM in models)
            {
                comboBox2.Items.Add(planeM.planeModelName);
                autoComplete.Add(planeM.planeModelName);
            }
            comboBox2.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox2.AutoCompleteCustomSource = autoComplete;
        }
    }
}
